﻿using QLXN.Common;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace QLXN
{
    public partial class Config : hTekForm
    {
        public Config()
        {
            InitializeComponent();
        }

        SqlInteraction sql = new SqlInteraction();

        private void Config_Load(object sender, EventArgs e)
        {
            MessageBox.Show(this, "Không thể kết nối tới máy chủ!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            display_info();
        }

        private void display_info()
        {
            try
            {
                Hashtable ht = new Hashtable();
                ht = sql.Open();
                if (ht.Count > 0)
                {
                    if (ht.ContainsKey("server"))
                    {
                        txt_sv.Text = ht["server"].ToString();
                        if (ht.ContainsKey("database"))
                        {
                            txt_db.Text = ht["database"].ToString();
                            if (ht.ContainsKey("username"))
                            {
                                txt_us.Text = ht["username"].ToString();
                                if (ht.ContainsKey("password"))
                                {
                                    txt_pw.Text = ht["password"].ToString();
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            { }
        }

        private void btn_save_Click(object sender, EventArgs e)
        {
            try
            {
                string sv = txt_sv.Text;
                string db = txt_db.Text;
                string us = txt_us.Text;
                string pw = txt_pw.Text;
                bool flag = sql.Save(sv, db, us, pw);
                if (flag)
                {
                    MessageBox.Show(this, "Lưu cấu hình thành công!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    this.Hide();
                    Application.Restart();
                }
            }
            catch (Exception ex)
            { }
        }

        private void btn_exit_Click(object sender, EventArgs e)
        {
            DialogResult rs = MessageBox.Show(this, "Thoát chương trình?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (rs == DialogResult.Yes)
                Application.Exit();
        }
    }
}
